package edu.suda.basic;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.safehaus.uuid.UUIDGenerator;

public class UUID
{
	public static String getMac() throws Exception
	{
		{
			InetAddress address = InetAddress.getLocalHost();
			NetworkInterface ni = NetworkInterface.getByInetAddress(address);
			byte[] mac = ni.getHardwareAddress();
			String MAC = "";
			for (int i = 0; i < mac.length; i++)
				MAC += String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
			return MAC;
		}
	}
	public static String bytes2String(byte[] mac)
	{
		if (mac != null)
		{
			String MAC = "";
			for (int i = 0; i < mac.length; i++)
				MAC += String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
			return MAC;
		}
		else
			return "";
	}
	public static String getUUID()
	{
		String primaryKey = UUIDGenerator.getInstance().generateRandomBasedUUID().toString();
		primaryKey = primaryKey.replaceAll("-", "");
		//System.out.println(primaryKey);
		return primaryKey;
	}

	public static List<String> getMacList() throws Exception
	{
		List<String> macList=new ArrayList<String>();
		Enumeration<NetworkInterface> enm = null;
		try
		{
			enm = NetworkInterface.getNetworkInterfaces();
		}
		catch (SocketException e1)
		{
			e1.printStackTrace();
		}
		
		while (enm.hasMoreElements())
		{
			NetworkInterface ni = enm.nextElement();
			
			if (UUID.bytes2String(ni.getHardwareAddress()).length()==17)
			{
				//System.out.print("interface:"+ni.toString());
				//System.out.println(UUID.bytes2String(ni.getHardwareAddress()));
				//System.out.println("---------------------------------------------------------");
				macList.add(UUID.bytes2String(ni.getHardwareAddress()));
			}
		}
		return macList;
		
	}
}

